package org.kisekiproject.course {
	import org.kisekiproject.evaluator.BindingExpression;
	import org.kisekiproject.evaluator.IExpression;

	/**
	 * A course action call.
	 */
	public class CourseActionCall {

		private var _course:CourseModel;
		private var _parameterExpressions:Object;
		private var _actionName:String;

		/**
		 * Constrcutor.
		 */
		public function CourseActionCall() {
		}

		/**
		 * Set course reference.
		 */
		public function set course(value:CourseModel):void {
			_course=value;
		}

		/**
		 * Parse.
		 */
		public function parse(source:XML):void {
			_actionName=String(source.name());
			_parameterExpressions=new Object();

			for each (var x:XML in source.attributes()) {
				var param:String=String(x.name());
				var expressionString:String=String(x.valueOf());
				var expression:IExpression=new BindingExpression(expressionString,_course.variables);

				_parameterExpressions[param]=expression;
			}
		}

		/**
		 * Perform the call.
		 */
		public function call():void {
			var parameters:Object=new Object();

			for (var s:String in _parameterExpressions) {
				var x:IExpression=_parameterExpressions[s];
				parameters[s]=x.value;
			}

			_course.actionController.callAction(_actionName,parameters);
		}
	}
}